import Dingo.API as api
import Dingo.sound as sound
import random

__author__ = "Sarah Mount"

### COLOUR CODE:
# WHITE: Sleeping
# GREEN: Sounding
# RED:   Listening
# BLUE:  Processing sounds

PATH = '/tmp/sensor/sound-test/' ### NOT PORTABLE!!

class TheSimulation(api.Simulation):
	def __init__(self, graph):
		ether = api.Ether(self)
		api.Ether.RF_radius = 250
		sound.Beep.radius = 250
		num_nodes = 5
		s_sensor = SoundingSensor(graph, ether)
		o_sensor = OutOfRangeSensor(graph, ether)
		sensors = [ListeningSensor(graph, ether, i) for i in xrange(num_nodes)]
		sensors.append(s_sensor)
		sensors.append(o_sensor)
		api.Simulation.__init__(self, sensors, graph)
		return
	def run(self):
		super(TheSimulation, self).run()

class SoundingSensor(api.Sensor, sound.SoundSensor):
	def __init__(self, graph, ether):
		id = 'SoundingSensor'
		x = random.randrange(150)
		y = random.randrange(150)
		col = api.SensorColor.White
		sched = api.Scheduler([ColourTask(0, 0, api.SensorColor.Green, self), sound.Sounder(1, 0, self), SleepTask(2, 0, self)])
		api.Sensor.__init__(self, graph, id, (x,y), col, sched, ether)
		sound.SoundSensor.__init__(self, id, PATH) 
		return

class ListeningSensor(api.Sensor, sound.SoundSensor):
	def __init__(self, graph, ether, i):
		id = "Sensor[%d]" % i
		x = random.randrange(250)
		y = random.randrange(250)
		col = api.SensorColor.Grey
		sched = api.Scheduler([ProcessSoundTask(0, 0, self), ColourTask(1, 0, api.SensorColor.Red, self), sound.Microphone(2, 0, self)])
		api.Sensor.__init__(self, graph, id, (x,y), col, sched, ether)
		sound.SoundSensor.__init__(self, id, PATH)
		return

class OutOfRangeSensor(api.Sensor, sound.SoundSensor):
	"""This sensor shouldn't hear a thing."""
	def __init__(self, graph, ether):
		id = "OutOfRange"
		x = 400
		y = 400
		col = api.SensorColor.Black
		sched = api.Scheduler([ProcessSoundTask(0, 0, self), sound.Microphone(1, 0, self)])
		api.Sensor.__init__(self, graph, id, (x,y), col, sched, ether)
		sound.SoundSensor.__init__(self, id, '/tmp/sensor/')
		return

class ProcessSoundTask(api.Task):
	"""Process sounds that the microphone leaves on the sound_q queue."""
	def __init__(self, priority, repetition, sensor):
		api.Task.__init__(self, 1, 0, sensor)
		return
	def task(self):
		self.sensor.node.SetNodeColor(api.SensorColor.Blue)
		while not self.sensor.sound_q.empty():
			obj = self.sensor.sound_q.get()
			print self.sensor.id, "heard", obj.__repr__()
		api.Sleep(random.uniform(1, 2))
		return

class ColourTask(api.Task):
	def __init__(self, priority, repetition, colour, sensor):
		self.colour = colour
		api.Task.__init__(self, priority, repetition, sensor)
		return
	def task(self):
		self.sensor.node.SetNodeColor(self.colour)
		api.Sleep(random.uniform(1, 2))
		return

class SleepTask(api.Task):
	def __init__(self, priority, repetition, sensor):
		api.Task.__init__(self, priority, repetition, sensor)
		return
	def task(self):
		self.sensor.node.SetNodeColor(api.SensorColor.White)
		api.Sleep(random.uniform(1, 3))
		return
